/*
 * Copyright � 2008 Nokia Corporation.
 */

// INCLUDE FILES
#include "SudokuGameOverDialog.h"
#include "SudokuAppUi.h"
#include "SudokuAppView.h"
#include "SudokuStatisticControl.h"
#include <eikenv.h>

const TInt KShowTime(5000000);

CSudokuGameOverDialog::CSudokuGameOverDialog()
    {
    
    iAppUi = static_cast<CSudokuAppUi*>(iEikonEnv->EikAppUi());
    
    // Closes this dialog after KShowTime 5s
    TRAPD(err,
            iPeriodic = CPeriodic::NewL(CActive::EPriorityStandard);
    );
    if (!err)
        {
        iPeriodic->Start(KShowTime, KShowTime, TCallBack(HideDialog, this));
        }
    }

CSudokuGameOverDialog::~CSudokuGameOverDialog()
    {
    if (iPeriodic)
        {
        iPeriodic->Cancel();
        }
    delete iPeriodic;
    }

void CSudokuGameOverDialog::PreLayoutDynInitL()
    {
    CAknQueryDialog::PreLayoutDynInitL();

    CSudokuContainerControl* view = iAppUi->View();
    view->StatisticControl()->StopGameTimer();
    
    SetEditableL(EFalse);    
    }

void CSudokuGameOverDialog::PostLayoutDynInitL()
    {
    // No CBA buttons
    CEikButtonGroupContainer* cba = CEikButtonGroupContainer::Current();
    cba->MakeVisible(EFalse);

    // Calculate position of the dialog
    CalculatePositionAndSize();
    }

TBool CSudokuGameOverDialog::OkToExitL(TInt /*aButtonId*/)
    {
    return ETrue;
    }

TInt CSudokuGameOverDialog::CountComponentControls() const
    {
    return( 0 ); 
    }

CCoeControl* CSudokuGameOverDialog::ComponentControl(TInt /*aIndex*/) const
    {
    return NULL;
    }

void CSudokuGameOverDialog::CalculatePositionAndSize()
    {
    TRect cRect;
    AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EScreen,cRect);
    
    if (cRect.Height()>cRect.Width())
        {
        cRect.iTl.iY = (cRect.Height() - cRect.Width())/2;
        cRect.SetHeight(cRect.Width());
        }
    else
        {
        cRect.iTl.iX = (cRect.Width() - cRect.Height())/2;
        cRect.SetWidth(cRect.Height());
        }

    cRect.Shrink(40,cRect.Height()/3);
    
    SetExtent(cRect.iTl,cRect.Size());
    }

void CSudokuGameOverDialog::SetSizeAndPosition(const TSize &aSize)
    {
    CAknQueryDialog::SetSizeAndPosition( aSize );
    CalculatePositionAndSize();
    }

void CSudokuGameOverDialog::SizeChanged()
    {
    TRect r = Rect();
    
    CSudokuContainerControl* view = iAppUi->View();
    CFont* font = view->StatisticFont();

    iLine1 = r.iTl;
    iLine2 = r.iTl;
    TInt txtWidth = font->TextWidthInPixels(_L("Sudoku completed"));
    iLine1.iX += (r.Width() - txtWidth) / 2;
    iLine2.iX += (r.Width() - txtWidth) / 2;
    
    TInt y = r.Height()/5;
    iLine1.iY = y*2;
    iLine2.iY = y*4;
    
    DrawNow();
    }

void CSudokuGameOverDialog::HandleResourceChange(TInt aType)
    {
    CAknQueryDialog::HandleResourceChange(aType);   
    }

void CSudokuGameOverDialog::Draw( const TRect& /*aRect*/ ) const
    {
    // Get the standard graphics context
    CWindowGc& gc = SystemGc();

    CSudokuContainerControl* view = iAppUi->View();
    CFont* font = view->StatisticFont();
    
    gc.UseFont(font);

    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.SetBrushColor(TRgb(255,243,206));
    gc.DrawRect(Rect());

    gc.SetPenColor(KRgbBlack);
    gc.DrawRect(Rect());
    
    gc.DrawText(_L("Sudoku completed"),iLine1);
    gc.DrawText(_L("Congratulations!"),iLine2);
    
    gc.DiscardFont();
    }

TInt CSudokuGameOverDialog::HideDialog(TAny* aAny)
    {
    // Closes dialog
    CSudokuGameOverDialog* self = static_cast<CSudokuGameOverDialog*>( aAny );

    self->iPeriodic->Cancel();
    
    TRAP_IGNORE(
            self->TryExitL(1);
            );
    return KErrNone;
    }


